/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.agileClinic.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import javax.ejb.EJBException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

/**
 * Classe do Projeto guardiao - Criado em 11/04/2013 -
 *
 * @author Gilmário
 */
public class DAO<T> extends DaoGenerico<T, Serializable> {

    @PersistenceContext
    private EntityManager em;

    public DAO(Class classe) {
        super(classe);
    }

    @Override
    public void salvar(Object t) throws SQLException, PersistenceException, EJBException, Exception {
        em.persist(t);
    }

    @Override
    public void deletar(Object t) throws SQLException, PersistenceException, EJBException, Exception {
        em.remove(t);
    }

    @Override
    public void atualizar(Object t) throws SQLException, PersistenceException, EJBException, Exception {
        em.merge(t);
    }

    @Override
    public T carregar(Serializable id) throws SQLException, PersistenceException, EJBException, Exception {
        return (T) em.find(getEntityClass(), id);
    }

    protected EntityManager getEm() {
        return em;
    }

    protected void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public List<T> listarTodos() throws SQLException, PersistenceException, EJBException, Exception {
        return em.createQuery("FROM " + getEntityClass().getName()).getResultList();
    }
}
